package com.cjr.ddp.controller;

import com.cjr.ddp.entity.DictTarget;
import com.cjr.ddp.entity.User;
import com.cjr.ddp.service.TarService;
import com.cjr.ddp.service.UserService;
import com.cjr.ddp.util.ReturnMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cjr
 * @date Created in 2020-03-07 22:04
 * @description 用户控制层
 * @modified By
 */
@RestController
@RequestMapping("/tar")
public class TarController {

    @Autowired
    TarService tarService;

    /**
     * @param user
     * @return com.cjr.ddp.util.ReturnMsg
     * @throws
     * @author cjr
     * @date 2020-03-07 22:15
     * @description 指标新增
     */
    @RequestMapping("/insertTar")
    public ReturnMsg insertTar(DictTarget dictTarget) {
        ReturnMsg returnMsg = new ReturnMsg();

        List<DictTarget> tarList = tarService.selectTarByTarName(dictTarget);
        if (tarList != null && tarList.size() != 0) {
            returnMsg.setCode("404");
            returnMsg.setSuccess(false);
            returnMsg.setMsg("该指标名已经存在，请更改!");
        } else {
            int insertBack = tarService.insertTar(dictTarget);
            if (insertBack > 0) {
                returnMsg.setCode("200");
                returnMsg.setSuccess(true);
                returnMsg.setMsg("新增指标成功!");
            } else {
                returnMsg.setCode("404");
                returnMsg.setSuccess(false);
                returnMsg.setMsg("新增指标失败，请重试!");
            }
        }

        return returnMsg;

    }

    /**
     * @param user
     * @return com.cjr.ddp.util.ReturnMsg
     * @throws
     * @author cjr
     * @date 2020-03-07 22:29
     * @description 指标删除
     */
    @RequestMapping("/deleteTarByTarID")
    public ReturnMsg deleteTarByTarID(DictTarget dictTarget) {

        ReturnMsg returnMsg = new ReturnMsg();

        int deleteBack = tarService.deleteTarByTarId(dictTarget.getId());
        if (deleteBack > 0) {
            returnMsg.setCode("200");
            returnMsg.setSuccess(true);
            returnMsg.setMsg("删除成功!");
        } else {
            returnMsg.setCode("404");
            returnMsg.setSuccess(false);
            returnMsg.setMsg("删除失败，请重试!");
        }
        return returnMsg;
    }


    /**
     * @param user
     * @return com.cjr.ddp.util.ReturnMsg
     * @throws
     * @author cjr
     * @date 2020-03-07 22:29
     * @description 指标修改
     */
    @RequestMapping("/updateTarByTarID")
    public ReturnMsg updateTarByTarID(DictTarget dictTarget) {

        ReturnMsg returnMsg = new ReturnMsg();
        int updateBack = tarService.updateTarByTarID(dictTarget);
        if (updateBack > 0) {
            returnMsg.setCode("200");
            returnMsg.setSuccess(true);
            returnMsg.setMsg("修改成功!");
        } else {
            returnMsg.setCode("404");
            returnMsg.setSuccess(false);
            returnMsg.setMsg("修改失败，请重试!");
        }
        return returnMsg;
    }


    /**
     * @param
     * @return java.util.List<com.cjr.ddp.entity.User>
     * @throws
     * @author cjr
     * @date 2020-03-07 22:33
     * @description 查询所有指标
     */
    @RequestMapping("/selectTarList")
    public List<DictTarget> selectTarList() {
        List<DictTarget> tarList = tarService.selectTarList();
        return tarList;
    }


}
